//
//  MyCollectViewController.m
//  Happy Kitchen
//
//  Created by 冫柒Yun on 15/3/12.
//  Copyright (c) 2015年 mac_chen. All rights reserved.
//

#import "MyCollectViewController.h"

@interface MyCollectViewController ()

@end

@implementation MyCollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"我的收藏";
    
    self.navigationController.navigationBar.translucent = NO;
    
    // 显示我的食疗收藏界面
    self.myFoodTherapyTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49)];
    
    // 设置tableview的tag值
    self.myFoodTherapyTableV.tag = 1001;
    
    // 指定代理人
    self.myFoodTherapyTableV.delegate = self;
    self.myFoodTherapyTableV.dataSource = self;
    
    [self.view addSubview:self.myFoodTherapyTableV];
    
    
    // 显示我的菜谱收藏界面
    self.myClassifyTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49)];
    
    // 设置tableview的tag值
    self.myClassifyTableV.tag = 1002;
    
    // 指定代理人
    self.myClassifyTableV.delegate = self;
    self.myClassifyTableV.dataSource = self;
    
    [self.view addSubview:self.myClassifyTableV];
    
    
    // 设置导航栏右侧按钮
    self.editButtonItem.title = @"编辑";
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    
    // 创建UISegmentedControl控件
    NSArray *array = @[@"我的菜谱", @"我的食疗"];
    
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:array];
    segmentedControl.frame = CGRectMake(0, 0, 150, 25);
    
    // 将UISegmentedControl添加到navigationItem上
    self.navigationItem.titleView = segmentedControl;
    
    // 切换界面
    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    segmentedControl.selectedSegmentIndex = 0;
    
    
}


// 重写系统编辑按钮触发方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.myClassifyTableV setEditing:editing animated:animated];
    [self.myFoodTherapyTableV setEditing:editing animated:animated];
    
    // 判断当前编辑按钮处于状态
    if (self.editing) {
        self.editButtonItem.title = @"完成";
    }else{
        self.editButtonItem.title = @"编辑";
    }
}
// 修改删除按钮的文字
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

// 编辑类型
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

// 允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

// 实现编辑逻辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (1002 == tableView.tag) {
        // 查询需要删除的model对象
        DetailModel *detaiModel = [[[ClassifyDataBase sharkClassifyDataBase] selectClassifyDetail] objectAtIndex:indexPath.row];
        // 删除数据库中包含model对象的其中一个属性的一个数据
        [[ClassifyDataBase sharkClassifyDataBase] deleteClassify:detaiModel.Title];
        
        // 删除cell
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
    }else if (1001 == tableView.tag){
        // 查询需要删除的model对象
        MenuDetail *menuDetil = [[[FoodDataBase shareMenuDataBaseHandle] selectMenu] objectAtIndex:indexPath.row];
        // 删除数据库中包含model对象的其中一个属性的一个数据
        [[FoodDataBase shareMenuDataBaseHandle] deleteMenu:menuDetil.name];
        
        // 删除cell
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

// cell的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (1002 == tableView.tag) {
        return [[ClassifyDataBase sharkClassifyDataBase] selectClassifyDetail].count;
    }
    
    return [[FoodDataBase shareMenuDataBaseHandle] selectMenu].count;
}

// cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdetifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdetifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdetifier];
    }
    
    if (1002 == tableView.tag) {
        cell.textLabel.text = [[[[ClassifyDataBase sharkClassifyDataBase] selectClassifyDetail] objectAtIndex:indexPath.row] Title];
    }else if (1001 == tableView.tag) {
        cell.textLabel.text = [[[[FoodDataBase shareMenuDataBaseHandle] selectMenu] objectAtIndex:indexPath.row] name];
    }
    
    // 设置每个cell后面都带有向右的图标, 显示点击还能进入下一层
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

// 点击cell触发事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (1002 == tableView.tag) {
        DetailViewController * detailVC = [[DetailViewController alloc] init];
        
        DetailModel *detailModel = [[[ClassifyDataBase sharkClassifyDataBase] selectClassifyDetail] objectAtIndex:indexPath.row];
        // 将title的值传到下个页面
        detailVC.titleStr = detailModel.Title;
        // 将RecipeId的值传到下一页面
        detailVC.rid = detailModel.RecipeId;
        
        [self.navigationController pushViewController:detailVC animated:YES];
    }else if (1001 == tableView.tag) {
        MenuDetailViewController *meniDetaiVC = [[MenuDetailViewController alloc] init];
        
        MenuDetail *menuDetail = [[[FoodDataBase shareMenuDataBaseHandle] selectMenu] objectAtIndex:indexPath.row];
        // 将name的值传给下一页面
        meniDetaiVC.name = menuDetail.name;
        // 将vegetable_id的值传给下一个页面
        meniDetaiVC.vegetable_id = menuDetail.vegetable_id;
        
        [self.navigationController pushViewController:meniDetaiVC animated:YES];
        
    }
    
}

// 实现切换界面的方法
- (void)segmentAction:(UISegmentedControl *)segmentedC
{
    if (0 == segmentedC.selectedSegmentIndex) {
        [self.view addSubview:self.myClassifyTableV];
    }else if (1 == segmentedC.selectedSegmentIndex){
        [self.view addSubview:self.myFoodTherapyTableV];
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
